// pages/business/business.js
const app = getApp()
const api = require('../../utils/util') 
Page({
    // 页面分享
    onShareAppMessage () {},
    // 分享朋友圈
    onShareTimeline () {},

    /**
     * 页面的初始数据
     */
    data: {
        imagesUrl: app.globalData.siteUrl,

        // 切换置顶
        //判断登录状态
        isLogin:false,
        tabsList: [
            {
                title: '需求'
            },
            {
                title: '服务'
            }
        ],
        tabsCurrent: 0,
        stickyScroll: false,
        value:'',
        business: {
          supply1: [],
          supply2: []
        },
        supply1_page: 1,
        supply2_page: 1
      },

    /**
     * 生命周期函数--监听页面加载
     */
    onShow() {

      let userinfo = app.globalData.userinfo;
      console.log(userinfo)
      if(app.globalData.userinfo){
        this.setData({
          isLogin:true
        })
      }

      this.setData({
        supply1_page: 1,
        supply2_page: 1
      });

      let param = {
        str: ''
      }
      let that = this
      let { business } = this.data
      api.business_supply1(param).then(function(res){
        business.supply1 = res.data
        that.setData({
           business
        })
      })

      api.business_supply2(param).then(function(res){
        business.supply2 = res.data
        that.setData({
           business
        })
      })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onTabsClick(e) {
      let tabsCurrent = e.target.dataset.index
      this.setData({tabsCurrent})
  },
   
  onChange(e){
    let value = e.detail;
    this.setData({
      value: value
    })
    if(value == ''){
      this.onRefresh();
    }
  },
  onSearch(){
    let param = {
      str: this.data.value
    }
    let that = this;
    let { business } = this.data
    api.business_supply1(param).then(function(res){
      business.supply1 = res.data
      that.setData({
         business
      })
    })

    api.business_supply2(param).then(function(res){
      business.supply2 = res.data
      that.setData({
         business
      })
    })
  },
  toLogin: function(){
    wx.redirectTo({
      url: '/pages/login/login',
    })
 },

 onPullDownRefresh:function(){
  this.onRefresh();
},
onRefresh:function(){
    let that = this
    //导航条加载动画
    wx.showLoading();
    wx.showNavigationBarLoading();
    let { business } = this.data
    let param = {
      str: this.data.value
    }
    setTimeout(function () {
      wx.hideNavigationBarLoading();
      //停止下拉刷新
      that.setData({
        supply1_page: 1,
        supply2_page: 1
      });
      api.business_supply1(param).then(function(res){
        business.supply1 = res.data
        that.setData({
           business
        })
      })
      api.business_supply2(param).then(function(res){
        business.supply2 = res.data
        that.setData({
           business
        })
      })
      wx.stopPullDownRefresh();
      wx.hideLoading();
    }, 2000);
  },

  onReachBottom:function(){
   wx.showLoading();
   let that = this
    // 加载更多，滑动到底部执行此方法
    let param = {
      str: this.data.value
    }
    let { business } = this.data
    if(this.data.tabsCurrent == 0){
      let supply1_page = this.data.supply1_page ;
      api.business_supply1(param, supply1_page).then(function(res){
        if (res.data.length>0){
          business.supply1 = supply1_page == 1 ? res.data : that.data.business.supply1.concat(res.data)
          that.setData({
            business,
            supply1_page: that.data.supply1_page + 1
          },()=>{
            wx.hideLoading();
          })
        }else{
            wx.hideLoading();
            wx.showToast({
              title: '没有更多数据了',
              ico: 'error'
            })
        }
      })
    }else{
      let supply2_page = this.data.supply2_page ;
      api.business_supply2(param, supply2_page).then(function(res){
        if (res.data.length>0){
          business.supply2 = supply2_page == 1 ? res.data : that.data.business.supply2.concat(res.data)
          that.setData({
            business,
            supply2_page: that.data.supply2_page + 1
          },()=>{
            wx.hideLoading();
          })
        }else{
           wx.hideLoading();
            wx.showToast({
              title: '没有更多数据了',
              ico: 'error'
            })
        }
      })
    }
    
},
onStickyScroll: function(){

},

toDetail: function(e){
  wx.navigateTo({
    url: '/pages/detail/detail3/detail3?id=' + e.currentTarget.dataset.index,
  })
},

toDetail1: function(e){
  wx.navigateTo({
    url: '/pages/detail/detail1/detail1?id=' + e.currentTarget.dataset.index,
  })
}

})